function formatDate(date) {   //当前日期
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('-')
}

function formatDateAfter(date) {   //当前日期后十年
    var year = date.getFullYear()+10
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('-')
}

function formatNumber(n) {  
    n = n.toString()
    return n[1] ? n : '0' + n
} 

function formatTime(time) {    //当前时间
  if (typeof time !== 'number' || time < 0) {
    return time
  }

  var hour = parseInt(time / 3600)
  time = time % 3600
  var minute = parseInt(time / 60)
  time = time % 60
  var second = time

  return ([hour, minute, second]).map(function (n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }).join(':')
}

function formatLocation(longitude, latitude) {     //当前位置
  if (typeof longitude === 'string' && typeof latitude === 'string') {
    longitude = parseFloat(longitude)
    latitude = parseFloat(latitude)
  }

  longitude = longitude.toFixed(2)
  latitude = latitude.toFixed(2)

  return {
    longitude: longitude.toString().split('.'),
    latitude: latitude.toString().split('.')
  }
}

//成功提示
function showSuccess(title = "成功啦", duration = 5000) {
    wx.showToast({
        title: title,
        icon: 'success',
        duration: (duration <= 0) ? 5000 : duration
    });
}
//loading提示
function showLoading(title = "请稍后", duration = 5000) {
    wx.showToast({
        title: title,
        icon: 'loading',
        duration: (duration <= 0) ? 5000 : duration
    });
}
//隐藏提示框
function hideToast() {
    wx.hideToast();
}

//显示带取消按钮的消息提示框
function alertViewWithCancel(title = "提示", content = "消息提示", confirm, showCancel = "true") {
    wx.showModal({
        title: title,
        content: content,
        showCancel: showCancel,
        success: function (res) {
            if (res.confirm) {
                confirm();
            }
        }
    });
}
//显示不取消按钮的消息提示框
function alertView(title = "提示", content = "消息提示", confirm) {
    alertViewWithCancel(title, content, confirm, false);
}

//错误提示
function showToptips(title = "成功啦", duration = 5000) {
    wx.showToast({
        title: title,
        //icon: '',
        image: '/images/icon/close.png',
        duration: (duration <= 0) ? 5000 : duration
    });
}

module.exports = {
  formatDate: formatDate, 
  formatDateAfter: formatDateAfter,
  formatTime: formatTime,
  formatLocation: formatLocation,
  showSuccess: showSuccess,
  showToptips: showToptips,
  showLoading: showLoading,
  hideToast: hideToast,
  alertViewWithCancel: alertViewWithCancel,
  alertView: alertView
}
